***************************************************************
*********  ��A���єԍ��̊ԈႢ���C��*********
***************************************************************

replace serial_number=99 if year==1981&city_code==11204&unit_code==71&household_code==54
replace serial_number=99 if year==1981&city_code==26100&unit_code==51&household_code==12&serial_number==1
replace serial_number=99 if year==1986&city_code==27100&unit_code==122&household_code==5&serial_number==53
replace serial_number=99 if year==1988&city_code==34204&unit_code==21&household_code==3&serial_number==45

********* �s�����R�[�h���ŐV�̂��̂ɓ��� ****************

replace city_code=40221 if city_code==40302
replace city_code=34213 if city_code==34322
replace city_code=4100 if city_code==4201
replace city_code=12100 if city_code==12201
replace city_code=8223 if city_code==8423
replace city_code=11244 if city_code==11204
replace city_code=12232 if city_code==12326
replace city_code=11100 if city_code==11244
replace city_code=32209 if city_code==32381
replace city_code=10449 if city_code==10446
replace city_code=11202 if city_code==11403
replace city_code=16211 if city_code==16203
replace city_code=17463 if city_code==17423
replace city_code=22100 if city_code==22201
replace city_code=22226 if city_code==22422
replace city_code=23233 if city_code==23341
replace city_code=24203 if city_code==24462
replace city_code=25214 if city_code==25464
replace city_code=28229 if city_code==28211
replace city_code=28446 if city_code==28441
replace city_code=30208 if city_code==30322
replace city_code=33211 if city_code==33344
replace city_code=34202 if city_code==34311
replace city_code=35216 if city_code==35209
replace city_code=42204 if city_code==42306
replace city_code=43202 if city_code==43202
replace city_code=46216 if city_code==46367
replace city_code=47214 if city_code==47206
replace city_code=4215 if city_code==4502
replace city_code=8235 if city_code==8482
replace city_code=20203 if city_code==20341
replace city_code=44214 if city_code==44325
replace city_code=15100 if city_code==15201
replace city_code=22130 if city_code==22202
replace city_code=26214 if city_code==26361

*********  ���т̊i�t����ύX���ē���
*********  �_�ы��Ƃ̒����J�n�𒲐�����1999�N7���ȑO��2�𓪂ɂ���
*********  �����̐��єԍ��͑��̐��т̉e�����󂯂�̂ō폜

replace household_code=200+household_code if period<m(1999m7)
replace household_code=floor(household_code/10)
replace household_code=household_code-5 if mod(household_code,10)>2
replace household_code=household_code-50 if household_code>30

***************************************************************
*********  ��A���єԍ��̊ԈႢ���C��*********
***************************************************************

replace serial_number=99 if year==1981&city_code==11204&unit_code==71&household_code==54
replace serial_number=99 if year==1981&month==8&city_code==26100&unit_code==51&household_code==12&serial_number==1
replace serial_number=99 if year==1986&city_code==27100&unit_code==122&household_code==5&serial_number==53
replace serial_number=99 if year==1988&city_code==34204&unit_code==21&household_code==3&serial_number==45

******** �P�ʋ�̒����J�n�����` ***************

gen start_month=floor(unit_code/10)

******** �������ƒ����J�n���̍��Œ����̉񐔂��` *********

gen interview=month-start_month+1 if month>=start_month
replace interview=(month+12)-start_month+1 if month<start_month

******** �����̉񐔂�6��ȏ�ł���΁A�P�ʋ悪2�x�ڂ̒����ł邱�Ƃ�������
******** �����J�n����6�J���x�点�A���̐��т̒����J�n���ɂ���

replace start_month=mod((start_month+(interview>6)*6),12)
replace start_month=12 if start_month==0

******** �����J�n�N���O�N�ł���ꍇ���l�����āA�����J�n�N���`

gen start_year=year-(start_month>month)

******** �����J�n���̕ύX�ɍ��킹�Ē����񐔂��C��

replace interview=interview-6 if interview>6

******** �������т̎��ۂ̒����񐔂��Ƃɔԍ���t����

sort start_year start_month city_code unit_code household_code serial_number year month
by start_year start_month  city_code unit_code household_code serial_number: gen n_interview=_n

******** ���������J�n���̐��тɏ����ԍ���U��A���j�[�N�ȃL�[�����蓖�Ă�

sort start_year start_month n_interview city_code interview
by start_year start_month n_interview: gen id_temp=_n if n_interview==1
recast long id_temp
replace id_temp=0 if n_interview>1

******** ���j�[�N�ȃL�[�𐢑тɋ��L������
egen id=max(id_temp), by(start_year start_month city_code unit_code household_code serial_number)

recast long id
replace id=start_year*1000000+start_month*10000+id
format id %10.0f

gen pref=floor(city_code/1000)
gen city_size=floor(city_code/100)-pref*10

*move period city_code
*move id city_code
*move interview city_code
*move n_interview city_code
*move pref city_code
*move city_size city_code

*drop start_month start_year id_temp unit_code household_code serial_number
drop start_month start_year id_temp household_code serial_number

*move num_day city_size
*move dow51 city_size
*move dow52 city_size
*move dow53 city_size
*move dow54 city_size
*move dow55 city_size
*move dow56 city_size
*move dow57 city_size
*move cpi total_income






